package com.imageim.yimirror.ui.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseFragment;
import com.imageim.yimirror.base.Constants;
import com.imageim.yimirror.bean.PersonalReportBean;
import com.imageim.yimirror.common.network.constants.NetConstants;
import com.imageim.yimirror.common.util.UserUtil;
import com.imageim.yimirror.net.NetPresenterImpl;
import com.imageim.yimirror.net.NetView;
import com.imageim.yimirror.ui.adapter.PersonalReportManAdapter;
import com.imageim.yimirror.ui.adapter.PersonalReportWomenAdapter;
import com.imageim.yimirror.ui.widght.EmptyView;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Created by Yuaihen.
 * on 2018/7/6
 * 穿衣指南Fragment
 */
public class DressGuideFragment extends BaseFragment implements NetView {

    @BindView(R.id.rv_dressGuide)
    RecyclerView mRvDressGuide;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;
    @BindView(R.id.empty_view)
    EmptyView mEmptyView;
    private PersonalReportWomenAdapter mPersonalReportWomenAdapter;
    private PersonalReportManAdapter mPersonalReportManAdapter;
    private NetPresenterImpl mNetPresenter;
    private HashMap<String, String> mMap;
    private static final int MSG_REFRESH = 0x001;
    private static final int MSG_LOAD_DATA = 0x002;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dress_guide;
    }

    @Override
    protected void initListener() {
        mNetPresenter = new NetPresenterImpl(this);
        mSmartRefresh.setRefreshHeader(new MaterialHeader(mContext));
        mSmartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mNetPresenter.getObject(mContext, NetConstants.URL.DRESS, mMap, PersonalReportBean.class, MSG_REFRESH);
            }
        });

        mEmptyView.setOnFinishBtnClickListener(this::getPersonalReport);
    }

    @Override
    protected void initData() {
        mRvDressGuide.setLayoutManager(new LinearLayoutManager(getContext()));

        if (UserUtil.getGender() == 1) {
            //1男性 2女性
            mPersonalReportManAdapter = new PersonalReportManAdapter(mContext);
            mRvDressGuide.setAdapter(mPersonalReportManAdapter);
        } else {
            mPersonalReportWomenAdapter = new PersonalReportWomenAdapter(mContext);
            mRvDressGuide.setAdapter(mPersonalReportWomenAdapter);
        }

        getPersonalReport();
    }


    /**
     * 获取用户形象报告
     */
    private void getPersonalReport() {
        if (mMap == null) {
            mMap = new HashMap<>(4);
        }
        mMap.put(Constants.PHOTO_NO, UserUtil.getPhotoNo());

        //temp
        //        mMap.put("photoNo", "CN20181204193048Dy43");
        //        mMap.put("mac", "AC83F3B47D82");
        //        mMap.put("token", "4dd652dc092743b78e8cdd72496ba1861543923025867");
        mNetPresenter.getObject(mContext, NetConstants.URL.DRESS, mMap, PersonalReportBean.class, MSG_LOAD_DATA);
    }


    @Override
    public void onLoadPre(int msgCode) {

    }

    @Override
    public void onLoadSuccess(Object result, int msgCode) {
        if (msgCode == MSG_REFRESH) {
            mSmartRefresh.finishRefresh();
        } else {
            hideEmptyView();
        }
        PersonalReportBean bean = (PersonalReportBean) result;
        if (bean != null) {
            PersonalReportBean.ListBean list = bean.getList();
            if (list != null) {
                if (UserUtil.getGender() == 1) {
                    //男性
                    mPersonalReportManAdapter.setData(list);
                } else {
                    //女性
                    mPersonalReportWomenAdapter.setData(list);
                }
            }
        }
    }

    @Override
    public void onLoadFail(int errorCode, String errorMsg, int msgCode) {
        if (msgCode == MSG_REFRESH) {
            mSmartRefresh.finishRefresh();
        } else {
            showEmptyView();
        }
        toast(errorCode + errorMsg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mNetPresenter != null) {
            mNetPresenter.onDestroy();
        }
    }

    private void showEmptyView() {
        if (mEmptyView != null) {
            if (mEmptyView.getVisibility() == View.GONE) {
                mEmptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void hideEmptyView() {
        if (mEmptyView != null) {
            if (mEmptyView.getVisibility() == View.VISIBLE) {
                mEmptyView.setVisibility(View.GONE);
            }
        }
    }

}
